package cj.web.yc.controllers;

import java.io.File;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
import cj.web.admin.constants.Constants;
import cj.web.admin.utils.DateUtils;
import cj.web.admin.utils.ReadConfigProperties;
import cj.web.admin.utils.ReturnUUID;
import cj.web.login.controllers.BaseController;
import cj.web.yc.domain.StackRoom;
import cj.web.yc.domain.StackRoomItem;
import cj.web.yc.service.StackRoomService;

@Path("/stackRoom")
public class StackRoomController extends BaseController{

	private static Logger logger = Logger.getLogger(StackRoomController.class.getName());
	
	@Autowired
	private StackRoomService stackRoomService;
	
	@Get("show")
	public String show(Model model,StackRoom stackRoom) {
		return "/views/yc/stackRoomList.vm";
	}

	@Post("getList")
	public String queryList(StackRoom stackRoom) {
		//当前页  
		stackRoom.setStart((stackRoom.getPage()-1)*stackRoom.getRows());
		stackRoom.setLength(stackRoom.getRows());
		
		JSONObject jo = new JSONObject();
		Map rep = stackRoomService.query(stackRoom);
		List<StackRoom> objs = (List<StackRoom>) rep.get("stackRoomlis");
		
		int size = (int) rep.get("size");
		jo.put("rows", objs);
		jo.put("total", size);
		return "@"+jo.toString();
	}
	
	@Get("createPage")
	public String createPage(Model model,@Param("categoryId") int categoryId){
		model.add("categoryId",categoryId);
		return "/views/yc/stackRoomDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,StackRoom stackRoom,HttpServletRequest request,
			@Param("uploadimage") MultipartFile file){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		stackRoom.setDeleteFlag(Constants.deleteFlag.deleteFlag_1);
		stackRoom.setCreateAt(DateUtils.getNowTime());
		stackRoom.setCreateUser(returnSessionObj(request).getAccount());
		stackRoom.setUpdateAt(DateUtils.getNowTime());
		stackRoom.setUpdateUser(returnSessionObj(request).getAccount());
		
		try{
			// 判断文件是否为空  
	        if (!file.isEmpty()) {  
	            try {  
	                // 文件保存路径  
	            	String path = ReadConfigProperties.getValue("uploadFilePath");
	            	//判断文件是否存在，不存在创建文件
	            	Map rep = ReturnUUID.getFolder(path);
	            	path = rep.get("allpath").toString();//所在的全路径
	            	//文件名
	                String fileName =  ReturnUUID.getName(file.getOriginalFilename()); 
	                // 转存文件  
	                file.transferTo(new File(path+"/"+fileName));  
	                jo.put("filePath", rep.get("filepath")+"/"+fileName);
	                
	                stackRoom.setCarouselImage(rep.get("filepath")+"/"+fileName);//文件路径
	            } catch (Exception e) {  
	                e.printStackTrace();  
	                jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
	            }  
	        } 
			
	        stackRoomService.create(stackRoom);
		}
		catch(Exception e){
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id){
		StackRoom stackRoom = stackRoomService.queryById(id);
		
		model.add("stackRoom", stackRoom);
		return "/views/yc/stackRoomEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,StackRoom stackRoom,HttpServletRequest request,
			@Param("uploadimage") MultipartFile file){
		JSONObject jo = new JSONObject();
		
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		stackRoom.setUpdateAt(DateUtils.getNowTime());
		stackRoom.setUpdateUser(returnSessionObj(request).getAccount());
		
		try {
			
			// 判断文件是否为空  
	        if (!file.isEmpty()) {  
	            try {  
	                // 文件保存路径  
	            	String path = ReadConfigProperties.getValue("uploadFilePath");
	            	//判断文件是否存在，不存在创建文件
	            	Map rep = ReturnUUID.getFolder(path);
	            	path = rep.get("allpath").toString();//所在的全路径
	            	//文件名
	                String fileName =  ReturnUUID.getName(file.getOriginalFilename()); 
	                // 转存文件  
	                file.transferTo(new File(path+"/"+fileName));  
	                jo.put("filePath", rep.get("filepath")+"/"+fileName);
	                
	                stackRoom.setCarouselImage(rep.get("filepath")+"/"+fileName);//文件路径
	            } catch (Exception e) {  
	                e.printStackTrace();  
	                jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
	            }  
	        } 
			
	        stackRoomService.update(stackRoom);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("Ids") String Ids){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		for(String id :Ids.split(",")){
			stackRoomService.deleteById(Integer.parseInt(id));
		}
		
		return "@"+jo.toString();
	}
	
	//----------Item
	@Post("getItemList")
	public String queryItemList(StackRoomItem stackRoomItem) {
		//当前页  
		stackRoomItem.setStart((stackRoomItem.getPage()-1)*stackRoomItem.getRows());
		stackRoomItem.setLength(stackRoomItem.getRows());
		
		JSONObject jo = new JSONObject();
		Map rep = stackRoomService.queryItem(stackRoomItem);
		List<StackRoomItem> objs = (List<StackRoomItem>) rep.get("stackRoomItemlis");
		
		int size = (int) rep.get("size");
		jo.put("rows", objs);
		jo.put("total", size);
		return "@"+jo.toString();
	}
	
	@Get("createItemPage")
	public String createItemPage(Model model,@Param("stackRoomId") int stackRoomId){
		StackRoom stackRoom = stackRoomService.queryById(stackRoomId);
		model.add("stackRoomId",stackRoomId);
		model.add("categoryId",stackRoom.getCategoryId());
		return "/views/yc/stackRoomItemDetail.vm";
	}
	
	@Post("createItem")
	public String createItem(Model model,StackRoomItem stackRoomItem,HttpServletRequest request,
			@Param("uploadimage") MultipartFile file){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		stackRoomItem.setDeleteFlag(Constants.deleteFlag.deleteFlag_1);
		stackRoomItem.setCreateAt(DateUtils.getNowTime());
		stackRoomItem.setCreateUser(returnSessionObj(request).getAccount());
		stackRoomItem.setUpdateAt(DateUtils.getNowTime());
		stackRoomItem.setUpdateUser(returnSessionObj(request).getAccount());
		
		try{
			// 判断文件是否为空  
	        if (!file.isEmpty()) {  
	            try {  
	                // 文件保存路径  
	            	String path = ReadConfigProperties.getValue("uploadFilePath");
	            	//判断文件是否存在，不存在创建文件
	            	Map rep = ReturnUUID.getFolder(path);
	            	path = rep.get("allpath").toString();//所在的全路径
	            	//文件名
	                String fileName =  ReturnUUID.getName(file.getOriginalFilename()); 
	                // 转存文件  
	                file.transferTo(new File(path+"/"+fileName));  
	                jo.put("filePath", rep.get("filepath")+"/"+fileName);
	                
	                stackRoomItem.setCarouselImage(rep.get("filepath")+"/"+fileName);//文件路径
	            } catch (Exception e) {  
	                e.printStackTrace();  
	                jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
	            }  
	        } 
			
	        stackRoomService.createItem(stackRoomItem);
		}
		catch(Exception e){
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("editItem")
	public String editItem(Model model,@Param("id") int id){
		StackRoomItem stackRoomItem = stackRoomService.queryItemById(id);
		
		model.add("stackRoomItem", stackRoomItem);
		return "/views/yc/stackRoomItemEdit.vm";
	}
	
	@Post("updateItem")
	public String updateItem(Model model,StackRoomItem stackRoomItem,HttpServletRequest request,
			@Param("uploadimage") MultipartFile file){
		JSONObject jo = new JSONObject();
		
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		stackRoomItem.setUpdateAt(DateUtils.getNowTime());
		stackRoomItem.setUpdateUser(returnSessionObj(request).getAccount());
		
		try {
			
			// 判断文件是否为空  
	        if (!file.isEmpty()) {  
	            try {  
	                // 文件保存路径  
	            	String path = ReadConfigProperties.getValue("uploadFilePath");
	            	//判断文件是否存在，不存在创建文件
	            	Map rep = ReturnUUID.getFolder(path);
	            	path = rep.get("allpath").toString();//所在的全路径
	            	//文件名
	                String fileName =  ReturnUUID.getName(file.getOriginalFilename()); 
	                // 转存文件  
	                file.transferTo(new File(path+"/"+fileName));  
	                jo.put("filePath", rep.get("filepath")+"/"+fileName);
	                
	                stackRoomItem.setCarouselImage(rep.get("filepath")+"/"+fileName);//文件路径
	            } catch (Exception e) {  
	                e.printStackTrace();  
	                jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
	            }  
	        } 
			
	        stackRoomService.updateItem(stackRoomItem);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
	
	@Get("deleteItemById")
	public String deleteItemById(Model model,@Param("Ids") String Ids){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		for(String id :Ids.split(",")){
			stackRoomService.deleteItemById(Integer.parseInt(id));
		}
		
		return "@"+jo.toString();
	}
}